/*
* @author  Oleg Varaksin (ovaraksin@googlemail.com)
* $$Id: UnwantedAttributeInspector.java 46 2011-01-14 21:58:05Z ovaraksin $$
*/

package com.googlecode.jsf2inspector.plugin.inspection;

import com.googlecode.jsf2inspector.plugin.commons.AbstractFaceletsInspector;
import com.googlecode.jsf2inspector.plugin.commons.InspectionMessage;
import com.googlecode.jsf2inspector.plugin.model.Attribute;
import com.googlecode.jsf2inspector.plugin.model.Tag;
import org.apache.maven.plugin.MojoExecutionException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class UnwantedAttributeInspector extends AbstractFaceletsInspector
{
    @Override
    public Set<File> getObjectsToInspect() throws MojoExecutionException {
        ProcessFaceletsMojo processFaceletsMojo = (ProcessFaceletsMojo) processMojo;
        Set<File> objects = processFaceletsMojo.getFaceletsFiles();

        return objects;
    }

    @Override
    public List<InspectionMessage> inspect(File object) throws MojoExecutionException {
        File facelet = object;
        Document doc;

        try {
            if (verboseOutput) {
                processMojo.getLog().debug("Parsing xhtml file '" + facelet + "' in order to find unwanted attributes ...");
            }
            
            doc = processMojo.getDocumentBuilder().parse(facelet);
        } catch (Exception e) {
            throw new MojoExecutionException("Error while parsing xhtml file '" + facelet + "'", e);
        }

        List<InspectionMessage> inspectionMsgList = new ArrayList<InspectionMessage>();

        for (Tag tag : getTags()) {
            NodeList nodes = doc.getElementsByTagName(tag.getName());
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);

                List<Attribute> attrs = tag.getAttributes();
                if (attrs == null || attrs.isEmpty()) {
                    continue;
                }

                for (Attribute attr : attrs) {
                    NamedNodeMap nnm = node.getAttributes();
                    if (nnm == null) {
                        continue;
                    }

                    Node item = nnm.getNamedItem(attr.getName());
                    if (item == null) {
                        continue;
                    }

                    if (attr.getValue() == null) {
                        inspectionMsgList.add(new InspectionMessage(facelet.toString(),
                            "Unwanted attribute '" + attr.getName() + "' was found for the tag '" + tag.getName() + "'"));
                        continue;
                    }

                    String strAttr = item.getNodeValue();
                    if (strAttr.matches(attr.getValue())) {
                        inspectionMsgList.add(new InspectionMessage(facelet.toString(),
                            "Unwanted attribute '" + attr.getName() + "' with value '" + attr.getValue() + "' (reg. expression) was found for the tag '" + tag.getName() + "'"));
                    }
                }
            }
        }

        return inspectionMsgList;
    }
}
